<template>
  <el-container style="height: 100vh">
    <el-aside class="layout-menu">
      <div class="layout-title">后台管理系统</div>
      <el-menu
        background-color="1e222d"
        text-color="#C1C6C8"
        :default-active="$route.path"
        active-text-color="#FF9907"
      >
        <template v-for="item in mainRoutes" :key="item.path">
          <el-menu-item v-if="!item.hidden" @click="routePath(item.path)" :index="`${item.path}`">
            <el-icon>
              <icon-menu />
            </el-icon>
            <span>{{ item.routeName }}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header class="layout-header">
        <el-row>
          <el-col :span="6" class="notice-container">
            <el-carousel height="60px" direction="vertical" :autoplay="true">
              <el-carousel-item v-for="item in 4" :key="item">
                <el-icon>
                  <info-filled />
                </el-icon>
                <span class="notice">这是一条公告</span>
              </el-carousel-item>
            </el-carousel>
          </el-col>
          <el-col :span="12"></el-col>
          <el-col :span="6" class="login-user">
            <el-icon :size="20">
              <message />
            </el-icon>
            <el-divider direction="vertical"></el-divider>
            <el-dropdown @command="handleCommand">
              <div class="el-dropdown-link">
                <el-avatar :src="`/api/upload/${user.avatarId}`" :size="50"></el-avatar>
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </div>

              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <span class="user-name">{{ user.realname }}</span>
          </el-col>
        </el-row>
      </el-header>

      <el-main class="main-container">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";
import { Message, Menu, Setting } from "@element-plus/icons";
import { mapState } from "vuex";
import Cookies from "js-cookie";
import { mainRoutes } from "@/router/index";

export default defineComponent({
  components: {
    Message,
    Setting,
    "icon-menu": Menu,
  },
  data() {
    return {
      mainRoutes: mainRoutes,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    handleCommand(command: string) {
      if (command === "logout") {
        Cookies.remove("token");
        localStorage.removeItem("user");
        this.$router.push("/login");
      }
    },
    routePath(url: string) {
      this.$router.push(url);
    },
  },
  mounted() {
  },
});
</script>

<style lang="scss" scoped>
.layout-menu {
  width: 200px;
  background: #1e222d;
  .layout-title {
    line-height: 60px;
    color: #c1c6c8;
    text-align: center;
  }
  ::v-deep .el-menu {
    border: none;
  }
}
.layout-header {
  height: 60px;
  color: #1b1b1b;
  ::v-deep .el-row {
    height: 100%;
  }
  ::v-deep .el-col {
    height: 100%;
  }
  .login-user {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    .el-dropdown-link {
      display: flex;
      align-items: center;
    }
    .user-name {
      margin-left: 10px;
      margin-right: 20px;
    }
  }
  .notice-container {
    line-height: 60px;
    padding-left: 20px;
  }
}
.main-container {
  background: #fafafa;
}
</style>
